﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Basic_mailsend : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["tid"] == null)
                Response.Redirect("../Error/Error.htm");
            else
            {
                if (Request.QueryString["to"] != null && Request.QueryString["id"] != null)
                {
                    this.txtReceiver.Text = Request.QueryString["to"].ToString();
                    this.HiddenField1.Value = Request.QueryString["id"].ToString().Trim();
                }
                bindDDL();
            }
        }
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlType.SelectedIndex>0)
        {
            this.ddlCollege.Enabled = false;
            this.ddlTname.Enabled = false;
            if (this.ddlType.SelectedValue == "2")
            {
                this.txtReceiver.Text = Session["depart"].ToString() + "的所有注册教职工";
                this.HiddenField1.Value = "2";
            }
            else if (this.ddlType.SelectedValue == "3")
            {
                this.txtReceiver.Text = "所有院系的学院管理员";
                this.HiddenField1.Value = "3";
            }
            else
            {
                this.txtReceiver.Text = "本系统所有用户";
                this.HiddenField1.Value = "4";
            }
        }
        else
        {
            this.ddlCollege.Enabled = true;
            this.ddlTname.Enabled = true;
            if (this.ddlType.SelectedValue == "1")
            {
                this.txtReceiver.Text = "";
                this.HiddenField1.Value = "";
            }
        }
    }

    protected void bindDDL()
    {
        //消息类型操作
        string usertype = Session["usertype"].ToString();
        if (usertype == "1")
            this.ddlType.Enabled = false;
        else if (usertype == "2")
        {
            this.ddlType.Items.RemoveAt(3);//移除系统消息
            this.ddlType.Items.RemoveAt(2);//移除项目消息
        }
        else if (usertype == "3")
        {
            this.ddlType.Items.RemoveAt(3);//移除系统消息
            this.ddlType.Items.RemoveAt(1);//移除学院消息
        }
        //绑定学院、教师
        PM.Code.Web.controlbind.BindDropDownList(this.ddlCollege, "select collegename from Dict_College", "collegename", "collegename");
        this.ddlCollege.Items.Insert(0,new ListItem("——选择学院——","0"));
    }
    //二级联动
    protected void ddlCollege_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlCollege.SelectedIndex != 0)
        {
            this.ddlTname.Items.Clear();
            string keyword = this.ddlCollege.SelectedValue.Trim();
            string sqlstr = " select tid,[name] from Sys_Account where depart='" + keyword + "'";
            PM.Code.Web.controlbind.BindDropDownList(this.ddlTname, sqlstr, "name", "tid");
            this.ddlTname.Items.Insert(0, new ListItem("选择教师", "0"));
        }
    }
    //选定检查
    protected void ddlTname_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedid = this.ddlTname.SelectedValue.Trim();
        string selectedname = this.ddlTname.SelectedItem.Text;
        if (selectedid == Session["tid"].ToString())
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "收信人不能是自己！");
            this.ddlTname.SelectedIndex = 0;
        }
        else
        {
            this.txtReceiver.Text = selectedname;
            this.HiddenField1.Value = selectedid;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //检测合法性
        if (Session["name"] == null)
            Response.Redirect("../Error/Error.htm");
        else if (this.txtReceiver.Text.Trim() == Session["name"].ToString().Trim())
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "不能发给自己！");
            return;
        }
        if (this.txtReceiver.Text == string.Empty || this.HiddenField1.Value.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "收信人不能为空！");
            return;
        }
        if (this.txtTitle.Text == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "消息标题不能为空！");
            return;
        }
        if (this.txtcontent.Text == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "消息内容不能为空！");
            return;
        }
        int result = 0;
        PM.PMBasic.Mail mail = new PM.PMBasic.Mail();
        string mailsender = Session["tid"].ToString();
        string mailtype = this.ddlType.SelectedValue.Trim();
        //先消息邮件内容，返回消息编号
        int id = mail.Insert_Mail(this.txtTitle.Text.Trim(), this.txtcontent.Text.Trim(),Int32.Parse(mailtype));
        //普通消息类型
        if (mailtype == "1")
            result = mail.Insert_Mail_User1(mailsender, this.HiddenField1.Value.Trim(), id);
        //学院消息类型
        else if (mailtype == "2")
        {
            string college = Session["depart"].ToString();
            result = mail.Insert_Mail_User2(mailsender, college, id);
        }
        //项目消息类型
        else if (mailtype == "3")
            result = mail.Insert_Mail_User3(mailsender, id);
        else if (mailtype == "4")
        {
            //不做处理，按照延迟加载策略进行
        }
        clearAll(result);
    }

    protected void clearAll(int row)
    {
        this.ddlType.SelectedIndex = 0;
        this.ddlCollege.Enabled = true;
        this.ddlTname.Enabled = true;
        this.ddlCollege.SelectedIndex = -1;
        this.ddlTname.SelectedIndex = -1;
        this.txtcontent.Text = "";
        this.txtReceiver.Text = "";
        this.HiddenField1.Value = "";
        this.txtTitle.Text = "";
        if(row>0)
            this.divnote.InnerHtml = "<img src=\"../js/themes/icons/ok.png\" alt=\"提示\" /><span id=\"errortip\">您的消息发送成功！</span>";
        else
            this.divnote.InnerHtml = "<img src=\"../js/themes/icons/no.png\" alt=\"提示\" /><span id=\"errortip\">您的消息发送失败，请重试！</span>";
    }
}
